#!/bin/env bash

# 设置时区
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai

# 关闭selinux
setenforce 0
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config

# 关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

# liunx内核配置调整
cat >> /etc/sysctl.conf <<EOF
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 加载配置文件
sysctl -p /etc/sysctl.conf

# 添加CentOS7镜像源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
cp /home/vagrant/yum/CentOS7-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo

# kubernetes镜像源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# create group if not exists
if [ ! $(getent group docker) ];
then 
    sudo groupadd docker;
else
    echo "docker user group already exists"
fi

usermod -aG docker vagrant
rm -rf ~/.docker/



# 安装kubernetes
# yum install -y --nogpgcheck kubelet kubeadm kubectl
# 启动开机自启
# systemctl enable --now kubelet

# Docker CE 镜像源
# step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo


# Step 3: 更新并安装Docker-CE
yum makecache fast
yum -y install docker-ce
# Step 4: 开启Docker服务
systemctl enable --now docker

# 配置docker仓库镜像加速
mkdir -p /etc/docker/
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors" : [
      "https://ajvcw8qn.mirror.aliyuncs.com",
      "http://2595fda0.m.daocloud.io"
    ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF

systemctl daemon-reload
systemctl restart docker

# 查看cgroupdriver
echo "show docker info message..."
docker info | grep Driver

# 安装常用工具
echo "install vim tree net-tools..."
yum install -y vim tree net-tools 